package cn.schoolwow.quickdao.module.database.dml.json.flow.common;

import cn.schoolwow.quickdao.domain.QuickDAOConfig;
import cn.schoolwow.quickdao.module.database.dml.common.domain.ManipulationOption;
import cn.schoolwow.quickflow.domain.FlowContext;
import cn.schoolwow.quickflow.flow.BusinessFlow;

public class SetDeleteJSONObjectStatementFlow implements BusinessFlow {
    @Override
    public void executeBusinessFlow(FlowContext flowContext) throws Exception {
        QuickDAOConfig quickDAOConfig = flowContext.checkInstanceData(QuickDAOConfig.class);
        String tableName = (String) flowContext.checkData("tableName");
        ManipulationOption manipulationOption = flowContext.checkInstanceData(ManipulationOption.class);

        StringBuilder sqlBuilder = new StringBuilder("delete from " + quickDAOConfig.databaseContext.databaseProvider.escape(tableName) + " where ");
        for(String uniqueFieldName:manipulationOption.uniqueFieldNames){
            sqlBuilder.append(quickDAOConfig.databaseContext.databaseProvider.escape(uniqueFieldName) + " = ? and");
        }
        sqlBuilder.delete(sqlBuilder.length()-4,sqlBuilder.length());
        flowContext.putTemporaryData("sql", sqlBuilder.toString());
    }

    @Override
    public String name() {
        return "设置删除JSONObject的SQL语句";
    }

}
